package tool

import (
	"fmt"
	"net"
	"os"
	"strings"
)

// GetIP 获取ip
func GetIP() string {
	var _ip = ""
	adds, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for _, address := range adds {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil && !strings.Contains(ipnet.IP.String(), "169.254") {
				_ip = ipnet.IP.String()
			}
		}
	}

	return _ip
}
